<?php
/*
 *
 * @authors : IT123编程社区-mastudy
 * @email  :admin@it123.org
 * @bbs http://www.it123.org
 *  @link http://www.it123.org
 *  @Powered By  ©2016 www.it123.org[IT123编程社区]
 */
// 本类由系统自动生成，仅供测试用途
class PublicAction extends CommonAction {
	
	
	
	
	

    Public function _initialize() {



        $session_name = session_name();

        if (isset($_POST[$session_name])) {

            session('[pause]');

            session(array('id' => $_POST[$session_name]));

            session("[start]");
        }
		
		
		
		       //热门文章
        $this->assign('hotArticle', $this->getArtlist(8,'article_visits desc'));
		
 

                //P($tags_info);
                
                
                $this->assign('taglists', ToolsAction::TopTags(100));
                
                
                
                
  
    }

    //添加收藏


    public function ajaxAddCollection() {


        $uid = ToolsAction::checkLogin();


        if (!$uid) {
            //请登录
            die(json_encode(array('status' => -1)));
        }



        $item_id = $this->_post('item_id');
        $module_id = $this->_post('module_id');




        if (!is_numeric($item_id)) {
            //参数错误
            die(json_encode(array('status' => -2)));
        }


        $res = ToolsAction::add_Collection($uid, $item_id, $module_id);


        if ($res == TRUE) {

            //ok
            die(json_encode(array('status' => TRUE)));
        } else if ($res == FALSE) {


            //关注失败
            die(json_encode(array('status' => FALSE)));
        } else {


            //已经关注过了
            die(json_encode(array('status' => 2)));
        }
    }

    //添加关注


    public function ajaxAddFollower() {


        $uid = ToolsAction::checkLogin();




        if (!$uid) {
            //请登录
            die(json_encode(array('status' => -1)));
        }



        $fuid = $this->_post('fuid');


        if (!is_numeric($fuid)) {
            //参数错误
            die(json_encode(array('status' => -2)));
        }

        $res = ToolsAction::add_Follower($uid, $fuid);


        if ($res == TRUE) {

            //ok
            die(json_encode(array('status' => TRUE)));
        } else if ($res == FALSE) {
            //关注失败
            die(json_encode(array('status' => FALSE)));
        } else {
            //已经关注过了
            die(json_encode(array('status' => 2)));
        }
    }

    public function addComment() {


        $uid = ToolsAction::checkLogin();

        if (!$uid) {
            $this->error('请登录后再操作！');
        }



        if (!ToolsAction::is_null_nickname($uid)) {

            $this->error('抱歉！填写昵称才能发表评论');
        }


        $comment_item_id = $this->_post('comment_item_id');


        $comment_module_id = $this->_post('comment_module_id');

        $comment_content = $this->_post('comment_content');





        if (!$comment_content) {
            $this->error('评论内容不能为空！');
        }






        $data['comment_uid'] = $uid;
        $data['comment_text'] = $comment_content;

        $data['comment_item_id'] = $comment_item_id;


        $data['comment_module_id'] = $comment_module_id;



        $data['comment_time'] = time();


        $cDB = M('comment');


        if ($cDB->add($data)) {



            $comment_author = ToolsAction::getUserinfo($uid, 'member_nickname');






            if ($comment_module_id == 1) {





                $title = ToolsAction::getArticleinfo($comment_item_id, 'article_title');



                $author_uid = ToolsAction::getArticleinfo($comment_item_id, 'article_author_id');

                $author_email = ToolsAction::getUserinfo($author_uid, 'member_email');

                ToolsAction::SendMail($author_email, 'www.it123.org提醒：区友' . $comment_author . '评论了您的文章《' . $title . '》，请及围观！', date('Y-m-d:h:m:s') . '区友' . $comment_author . '评论了您的文章《' . $title . '》，请及时围观');


                ToolsAction::SendMail('1261639911@qq.com', 'www.it123.org提醒：区友' . $comment_author . '评论了文章《' . $title . '》，请及围观并审核！', date('Y-m-d:h:m:s') . '区友' . $comment_author . '评论了文章《' . $title . '》，请及时围观并审核');

                //提醒邮件
            }


            if ($comment_module_id == 7) {






                $text = ToolsAction::getHeartenInfo($comment_item_id, 'hearten_text');




                $author_uid = ToolsAction::getHeartenInfo($comment_item_id, 'hearten_uid');



                $author_email = ToolsAction::getUserinfo($author_uid, 'member_email');





                ToolsAction::SendMail($author_email, 'www.it123.org提醒：区友' . $comment_author . '评论了您发的签到《' . $text . '》，请及围观！', date('Y-m-d:h:m:s') . '区友' . $comment_author . '评论了您的签到《' . $text . '》，请及时围观');


                ToolsAction::SendMail('1261639911@qq.com', 'www.it123.org提醒：区友' . $comment_author . '评论了签到《' . $text . '》，请及围观并审核！', date('Y-m-d:h:m:s') . '区友' . $comment_author . '评论了签到《' . $text . '》，请及时围观并审核');

                //提醒邮件
            }








            $this->success('ok！');
        } else {
            $this->error('sorry！');
        }
    }

    public function groupMasterPK() {



        $group_id = $this->_get('id');


        if (!is_numeric($group_id)) {

            $this->error('非法操作！');
        }




        $uid = ToolsAction::checkLogin();

        if (!$uid) {

            $this->error('登录后再再参与竞选吧！');
        }



        if (!ToolsAction::is_groupMember($group_id, $uid)) {

//添加成员

            ToolsAction::addGroupMemberDo($group_id, $uid);
        }





        if (ToolsAction::Is_GroupMaster($uid)) {


            $this->error('你已经是版主,不能再参与竞选！');
        }







        $user_heat = ToolsAction::getUserinfo($uid, 'member_heat');


        $master_uid = ToolsAction::getGroupInfo($group_id, 'group_master_id');



        $master_heat = ToolsAction::getUserinfo($master_uid, 'member_heat');


        if ($user_heat > $master_heat) {




            ToolsAction::updateGroupMaster($uid, $group_id);





            //tip

            $author = ToolsAction::getUserinfo($uid, 'member_nickname');

            $group_name = ToolsAction::getGroupInfo($group_id, 'group_name');




            $master_name = ToolsAction::getUserinfo($master_uid, 'member_nickname');


            $author_email = ToolsAction::getUserinfo($uid, 'member_email');

            $group_master_email = ToolsAction::getUserinfo($master_uid, 'member_email');





            //提醒邮件
            ToolsAction::SendMail($group_master_email, 'www.it123.org提醒：区友' . $author . '通过竞选PK版主获胜！请及围观！', date('Y-m-d:h:m:s') . '区友' . $author . '通知竞选PK版主获胜！成为' . $group_name . '新版主，圈友' . $author . '的威望值：' . $master_heat . ',您的威望值：' . $user_heat . '欢迎去PK再次夺取版主！');


            //提醒邮件
            ToolsAction::SendMail('1261639911@qq.com', 'www.it123.org提醒：区友' . $author . '通过竞选PK版主获胜！请及围观！', date('Y-m-d:h:m:s') . '区友' . $author . '通知竞选PK版主获胜！成为' . $group_name . '新版主');

            //提醒邮件
            ToolsAction::SendMail($author_email, 'www.it123.org提醒：您通过竞选PK版主获胜！请及围观！', date('Y-m-d:h:m:s') . '您通知竞选PK版主获胜！成为' . $group_name . '新版主，请继续加油！');




            $this->success('PK胜利！版主威望值：' . $master_heat . ',你威望值：' . $user_heat);

            die();
        } else {


            $this->error('PK失败！版主威望值：' . $master_heat . ',你威望值：' . $user_heat);
        }
    }

    public function addGroupMember($group_id) {


        $uid = ToolsAction::checkLogin();
        if (!$uid) {

            $this->error('请登录');
        }

        $id = $group_id;


        if (ToolsAction::is_groupMember($id, $uid)) {


            $this->error('您已经是本圈子成员，无需再加入！');
        }



        if (ToolsAction::addGroupMemberDo($id, $uid)) {

            $this->success('加入圈子成功！');
        } else {
            $this->error('申请失败！');
        }
    }

  

    public function addGroup() {


        $uid = session('member.member_id');

        if (!$uid) {

            $this->error('请登录后，再操作！');
        }



        if (!ToolsAction::is_null_nickname($uid)) {

            $this->error('抱歉！填写昵称后能创建板块');
        }



        $member_heat = session('member.member_heat');



        if ($member_heat < 100) {

            $this->error('威望值大于100才能够创建板块，您的威望值' . $member_heat . '继续加油吧！');
        }


        $group_title = $this->_post('group_name');


        $group_desc = $this->_post('group_desc');


        if (!$group_title) {
            $this->error('板块名称必须填写');
        }

        if (!$group_desc) {
            $this->error('板块介绍必须填写');
        }


        /*   if(ToolsAction::Is_GroupMaster($uid)){


          $this->error('每人只能创建一个板块的权限！,你已经是版主');
          }

         */

        $groupDB = M('group');


        $where['group_master_id'] = $uid;



        $where_group_name['group_title'] = $group_title;


        $num2 = $groupDB->where($where_group_name)->count();



        if ($num2 > 0) {

            $this->error('板块名称已经存在请更换了');
        }






        $data['group_master_id'] = $uid;
        $data['group_title'] = $group_title;
        $data['group_desc'] = $group_desc;
        $data['group_time'] = time();

        $new_group_id = $groupDB->add($data);



        if ($new_group_id > 0) {

            ToolsAction::addGroupMemberDo($new_group_id, $uid);




            $author = ToolsAction::getUserinfo($uid, 'member_nickname');




            $author_email = ToolsAction::getUserinfo($uid, 'member_email');

            ToolsAction::SendMail($author_email, 'www.it123.org提醒：您申请的《' . $group_title . '》板块创建成功！，请及围观并审核！', date('Y-m-d:h:m:s') . '您申请的《' . $group_title . '》板块创建成功，请及时围观！');



            ToolsAction::SendMail('1261639911@qq.com', 'www.it123.org提醒：' . $author . '申请的《' . $group_title . '》板块创建成功！，请及围观并审核！', date('Y-m-d:h:m:s') . $author . '申请的《' . $group_title . '》板块创建成功，请及时围观！');


            //提醒邮件











            $this->success($group_title . '恭喜板块创建成功,请等待管理员审核！');


            die();
        } else {
            $this->error($group_title . '板块创建失败！');
        }
    }

    public function addFeedback() {




        $user_id = session('member.member_id');



        if (!$user_id) {

            $this->error('请登录后再操作！');
        }

        $feedback_title = $this->_post('feedback_title');

        $feedback_text = $this->_post('feedback_text');

        $feedback_url = $this->_post('feedback_url');

        $dDB = M('feedback');


        $where['feedback_uid'] = $user_id;
        $where['feedback_time'] = array('gt', time() - $timeline);
        $num = $dDB->where($where)->count();

        if ($num > 3) {

            $this->error('每天只能反馈3次！');
        }





        $data['feedback_uid'] = $user_id;
        $data['feedback_title'] = $feedback_title;
        $data['feedback_text'] = $feedback_text;
        $data['feedback_url'] = $feedback_url;
        $data['feedback_time'] = time();
        if ($dDB->add($data)) {







            $this->success('感谢您的反馈！');
        } else {
            $this->error('sorry！');
        }
    }

    public function upUserWxpayImg() {


        $user_id = ToolsAction::checkLogin();


        if (!$user_id) {

            return FALSE;
        }


        $source_pic = $_FILES["__source"];

        $dir = './Data/Uploads/Userpayimg/';

        $filename = 'Userwxpay' . '_' . $user_id;

//头像图片(file 域的名称：__avatar1,2,3...)。
        $avatars = array("__avatar1", "__avatar2");
        $size_str = array('180', '100');
        $avatars_length = count($avatars);
        for ($i = 0; $i < $avatars_length; $i++) {
            $avatar = $_FILES[$avatars[$i]];

            $avatar_number = $size_str[$i];
            if ($avatar['error'] > 0) {
                $msg .= $avatar['error'];
            } else {
//$savePath ="$dir.$filename.jpg";
                $savePath = "$dir/" . $filename . '_' . $avatar_number . ".jpg";


                $result['avatarUrls'][$i] = $this->toVirtualPath($savePath);


                move_uploaded_file($avatar["tmp_name"], $savePath);



                $success_num++;
            }
        }





        ToolsAction::updateUserpayimg($user_id, 'wxpay');



        $result['msg'] = $msg;
        if ($success_num > 0) {

            $result['msg'] = true;
            $result['success'] = true;
        }



//返回图片的保存结果（返回内容为json字符串）
        print json_encode($result);
    }

    public function upUserPayImg() {





        $user_id = ToolsAction::checkLogin();


        if (!$user_id) {

            return FALSE;
        }


        $source_pic = $_FILES["__source"];

        $dir = './Data/Uploads/Userpayimg/';

        $filename = 'Userpay' . '_' . $user_id;

//头像图片(file 域的名称：__avatar1,2,3...)。
        $avatars = array("__avatar1", "__avatar2");
        $size_str = array('180', '100');
        $avatars_length = count($avatars);
        for ($i = 0; $i < $avatars_length; $i++) {
            $avatar = $_FILES[$avatars[$i]];

            $avatar_number = $size_str[$i];
            if ($avatar['error'] > 0) {
                $msg .= $avatar['error'];
            } else {
//$savePath ="$dir.$filename.jpg";
                $savePath = "$dir/" . $filename . '_' . $avatar_number . ".jpg";


                $result['avatarUrls'][$i] = $this->toVirtualPath($savePath);


                move_uploaded_file($avatar["tmp_name"], $savePath);



                $success_num++;
            }
        }





        ToolsAction::updateUserpayimg($user_id);


        $result['msg'] = $msg;
        if ($success_num > 0) {

            $result['msg'] = true;
            $result['success'] = true;
        }



//返回图片的保存结果（返回内容为json字符串）
        print json_encode($result);
    }

    public function upfaceimg() {


        $user_id = session('member.member_id');

        $source_pic = $_FILES["__source"];

        $dir = './Data/Uploads/Face/';

        $filename = 'face' . '_' . $user_id;

//头像图片(file 域的名称：__avatar1,2,3...)。
        $avatars = array("__avatar1", "__avatar2", "__avatar3");
        $size_str = array('100', '50', '32');
        $avatars_length = count($avatars);
        for ($i = 0; $i < $avatars_length; $i++) {
            $avatar = $_FILES[$avatars[$i]];

            $avatar_number = $size_str[$i];
            if ($avatar['error'] > 0) {
                $msg .= $avatar['error'];
            } else {
//$savePath ="$dir.$filename.jpg";
                $savePath = "$dir/" . $filename . '_' . $avatar_number . ".jpg";


                $result['avatarUrls'][$i] = $this->toVirtualPath($savePath);


                move_uploaded_file($avatar["tmp_name"], $savePath);



                $success_num++;
            }
        }
//<---------------处理头像图片结束
//upload_url中传递的额外的参数，如果定义的method为get请换为$_GET
//更新数据库member_up_face

        $mDB = M('member_basic');

        $data_member['member_id'] = $user_id;
        $data_member['member_up_face'] = 1;
        $mDB->save($data_member);

        ToolsAction::updateMemberSession($user_id);






        $result['msg'] = $msg;
        if ($success_num > 0) {



            $result['msg'] = true;
            $result['success'] = true;
        }








//返回图片的保存结果（返回内容为json字符串）
        print json_encode($result);
    }

    /*     * ************************************************************
     *  生成指定长度的随机码。
     *  @param int $length 随机码的长度。
     *  @access public
     * ************************************************************ */

    function createRandomCode($length) {
        $randomCode = "";
        $randomChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        for ($i = 0; $i < $length; $i++) {
            $randomCode .= $randomChars { mt_rand(0, 35)};
        }
        return $randomCode;
    }

    /*     * ************************************************************
     *  将物理路径转为虚拟路径。
     *  @param string $physicalPpath 物理路径。
     *  @access public
     * ************************************************************ */

    function toVirtualPath($physicalPpath) {
        $virtualPath = str_replace($_SERVER['DOCUMENT_ROOT'], "", $physicalPpath);
        $virtualPath = str_replace("\\", "/", $virtualPath);
        return $virtualPath;
    }

    public function ajaxUpdateLikeNumber() {





        $id = $this->_post('id');
        $module_id = $this->_post('module_id');

        $uid = ToolsAction::checkLogin();



        if (!$uid) {

            //没有登录
            die(json_encode(array('status' => -1)));
        }



        $lDB = M('like');

        $data['like_item_id'] = $id;
        $data['like_member_id'] = $uid;
        $data['like_module_id'] = $module_id;

        if ($lDB->where($data)->find()) {

//顶过
            die(json_encode(array('status' => -2)));
        } else {



            $data['like_time'] = time();
            if ($lDB->add($data)) {
                die(json_encode(array('status' => true)));
            } else {
                die(json_encode(array('status' => false)));
            }
        }
    }

    public function uploadfile() {



        import('ORG.Net.UploadFile');
        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize = 30000; // 设置附件上传大小
        $upload->allowExts = array('mp4', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->savePath = './Data/Uploads/'; // 设置附件上传目录
        if (!$upload->upload()) {// 上传错误提示错误信息
            $this->error($upload->getErrorMsg());
        } else {// 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();



            if ($info) {
// 上传成功 获取上传文件信息  
                foreach ($info as &$file) {
//拼接出文件相对路径
                    $file['filepath'] = $file['savepath'] . $file['savename'];
                }
//返回json数据被百度Umeditor编辑器  
                echo json_encode(array(
                    'url' => '/Data/Uploads/' . $file['savename'],
                    'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $file['savename'],
                    'state' => 'SUCCESS'
                ));
            } else {
// 上传失败  
                echo json_encode(array('state' => $upload->getError()));
            }
        }
    }

}
